package Util;

import org.springframework.stereotype.Component;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;

/**
 * Created by studio on 2017/3/23.
 */

@Component
public class SocketServer {

    private ServerSocket server = null;
    private Socket socket = null;
    private String line;
    private BufferedReader in;
    private PrintWriter writer;

    public void server_start(){

        try{
            Integer port = 1234;

            try {
                server = new ServerSocket(port);//服务器端socket

                System.out.println("服务器已启动，监听端口：" + port);
            }catch (Exception e){
                System.out.println("没有启用监听：" + port);
            }


            try {
                server.setSoTimeout(10000);//10秒内没有接受到连接就断开
                socket = server.accept();//阻塞，等待客户端连接，socket为通道

            }catch (Exception e){
                System.out.println("Error:" + e);
            }

        }catch (Exception e){
            System.out.println("Error:"+e);
        }

    }

    public void server_end(){
        try{
            writer.close();
            in.close();
            socket.close();
            server.close();
        }catch (Exception e){
            System.out.println("Error:"+e);
        }
    }

    public void server_accept(){
        try{
            //找时间了解io
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            writer = new PrintWriter(socket.getOutputStream());
//            System.out.println("Client:" + in.readLine());
            line = in.readLine();
//            line = "你好，旅行者";

            while(!line.equals("end")){

                if(line.equals("tasklist")){

                    // 显示进程列表
                    ProcessBuilder pb = new ProcessBuilder("tasklist");
                    Process p = pb.start();
                    BufferedReader out = new BufferedReader(new InputStreamReader(new BufferedInputStream(p.getInputStream()), Charset.forName("GB2312")));
                    BufferedReader err = new BufferedReader(new InputStreamReader(new BufferedInputStream(p.getErrorStream())));
                    line = "Window 系统进程列表\n";
                    String ostr;

                    while ((ostr = out.readLine()) != null)
                        line += (ostr + "\n");
                    String estr = err.readLine();
                    if (estr != null) {
                        System.out.println("\nError Info");
                        System.out.println(estr);
                    }

                    line += "end";
                }else if (line.equals("taskkill")){

                    //杀死进程 Windows
                    ProcessBuilder pb = new ProcessBuilder("taskkill","/PID","2144");
                    Process p = pb.start();
                    BufferedReader out = new BufferedReader(new InputStreamReader(new BufferedInputStream(p.getInputStream()), Charset.forName("GB2312")));
                    BufferedReader err = new BufferedReader(new InputStreamReader(new BufferedInputStream(p.getErrorStream())));
                    line = "杀死进程\n";
                    String ostr;

                    while ((ostr = out.readLine()) != null)
                        line += (ostr + "\n");
                    String estr = err.readLine();
                    if (estr != null) {
                        System.out.println("\nError Info");
                        System.out.println(estr);
                    }

                    line += "end";

                }else if(line.equals("ping_other")){
                    //连接其他主机
                    ProcessBuilder pb = new ProcessBuilder("ping","192.168.1.102");
                    Process p = pb.start();
                    BufferedReader out = new BufferedReader(new InputStreamReader(new BufferedInputStream(p.getInputStream()), Charset.forName("GB2312")));
                    BufferedReader err = new BufferedReader(new InputStreamReader(new BufferedInputStream(p.getErrorStream())));
                    line = "连接地址：192.168.1.102\n";
                    String ostr;

                    while ((ostr = out.readLine()) != null)
                        line += (ostr + "\n");
                    String estr = err.readLine();
                    if (estr != null) {
                        System.out.println("\nError Info");
                        System.out.println(estr);
                    }

                    line += "end";
                }else if (line.equals("other_tasklist")){
                    //查看其他主机进程tasklist 需要正确参数
                    ProcessBuilder pb = new ProcessBuilder("tasklist","/s","192.168.1.102","/u","username","/p","password");
                    Process p = pb.start();
                    BufferedReader out = new BufferedReader(new InputStreamReader(new BufferedInputStream(p.getInputStream()), Charset.forName("GB2312")));
                    BufferedReader err = new BufferedReader(new InputStreamReader(new BufferedInputStream(p.getErrorStream())));
                    line = "连接地址：192.168.1.102\n进程列表：\n";
                    String ostr;

                    while ((ostr = out.readLine()) != null)
                        line += (ostr + "\n");
                    String estr = err.readLine();
                    if (estr != null) {
                        System.out.println("\nError Info");
                        System.out.println(estr);
                    }

                    line += "end";
                }

                writer.println(line);
                writer.flush();
                line = in.readLine();
//                System.out.println("服务端显示：Server:" + line);
                System.out.println("Client:" + line);
            }
        }catch (Exception e){
            System.out.println("Error:"+e);

        }
    }
}
